package com.example.eshop_ssm.controller;

import com.example.eshop_ssm.annotation.CheckLogin;
import com.example.eshop_ssm.service.BookTrashService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/BookTrashController")
public class BookTrashController {
    @Autowired
    private BookTrashService bookTrashService;

    /**
     * 查询垃圾书本
     * @param page
     * @throws ServletException
     * @throws IOException
     * @return
     */
    @RequestMapping("/select")
    @ResponseBody
    @CheckLogin
    public ModelAndView selectBookInfo(String page) {
        Map<String, Object> bookAndPageCountMap = bookTrashService.selectBookInfo(page);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("bookAndPageCountMap",bookAndPageCountMap);
        //req.setAttribute("bookAndPageCountMap",bookAndPageCountMap);
        //req.getRequestDispatcher("WEB-INF/jsp/booktrash.jsp").forward(req,resp);
        modelAndView.setViewName("booktrash");
        return modelAndView;
    }

    /**
     * 单击删除书本
     * @param book_id
     * @throws ServletException
     * @throws IOException
     * @return
     */
    @CheckLogin
    @RequestMapping("/deleteBook/{book_id}")
    @ResponseBody
    public HashMap<String, Integer> deleteBook(@PathVariable("book_id") Integer book_id) {
        Integer integer = bookTrashService.deleteBook(book_id);
        HashMap<String, Integer> effectRow = new HashMap<>();
        effectRow.put("effectRow",integer);

//        ModelAndView modelAndView = new ModelAndView();
//        modelAndView.addObject("effectRowMap",effectRowMap);
//        modelAndView.setViewName("booktrash");
        return effectRow;
    }

    /**
     * 还原书本
     * @param book_id
     * @return
     */
    @CheckLogin
    @RequestMapping("/updateBook/{book_id}")
    @ResponseBody
    public HashMap<String, Integer>  updateBook(@PathVariable("book_id") String book_id) {
        Integer integer = bookTrashService.updateBook(book_id);

        HashMap<String, Integer> effectRow = new HashMap<>();
        effectRow.put("effectRow",integer);
        return effectRow;
    }

    /**
     * 批量操作  根据需要的操作字符串 进行相应的操作
     * @param is_garbageOrDelete
     * @param bookIds
     * @throws ServletException
     * @throws IOException
     * @return
     */
    @CheckLogin
    @RequestMapping("/restoreOrDelete")
    @ResponseBody
    public HashMap<String, Object> restoreOrDelete( String is_garbageOrDelete,String bookIds){
        Integer integer = bookTrashService.restoreOrDelete(is_garbageOrDelete, bookIds);
        HashMap<String, Object> effectRowMap = new HashMap<>();
        effectRowMap.put("effectRow",integer);

        return effectRowMap;

    }
}
